//
//  MineAllViewController.m
//  shanHaiHuiApp
//
//  Created by DS on 2018/3/16.
//  Copyright © 2018年 徐双. All rights reserved.
//

#import "MineAllViewController.h"
#import "MineTableViewCell.h"
#import "MineDispatchModel.h"
#import "OrderDetailViewController.h"

@interface MineAllViewController ()<UITableViewDelegate,UITableViewDataSource>

Strong UITableView * tableView;
Strong UIView * blankView;

Strong NSMutableArray * dataArr;


@end

//网络
@implementation MineAllViewController (NetWorking)

- (void)getMineData{
    [self showprogressHUD];
    NSDictionary *parameters = @{
                                 @"keyWord":self.textVc1,
                                 @"diststatus":self.dispatchVc1
                                 };
    [[AFAppDotNetAPIClient sharedClient] POST:@"morderList.do" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"code"] intValue] == 0) {
            for (NSDictionary * dic in responseObject[@"orderList"]) {
                
                
                MineDispatchModel * disPatchModel = [[MineDispatchModel alloc] init];
                [disPatchModel mineDispatchModelWithDic:dic];
                [self.dataArr addObject:disPatchModel];
                [self.view addSubview:self.tableView];
            }
            
        }else{
            ShowMessage(responseObject[@"msg"]);
            
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

@implementation MineAllViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self NavColor];
    [self getMineData];
}
// 导航颜色
- (void)NavColor{
    
    UINavigationBar * bar = self.navigationController.navigationBar;
    UIImage *bgImage = [UIImage imageWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64) WihtColor:getColor(navColor) alphe:1.0];
    [bar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
    
    
    [bar setShadowImage:[UIImage imageWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0.1) WihtColor:[UIColor blackColor] alphe:1.0]];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.dataArr.count == 0) {
        [self.view addSubview:self.blankView];
    }else{
    [self.view addSubview:self.tableView];
    }
    
    
}

#pragma mark - init
-(UIView *)blankView{
    if (!_blankView) {
        _blankView = [[UIView alloc] initWithFrame:CGRectMake(0, 42 , SCREEN_WIDTH, SCREEN_HEIGHT - height64 - 42)];
        UIImageView * blankImage = [UIImageView new];
        blankImage.image = [UIImage imageNamed:@"kongzhuangtai"];
        blankImage.contentMode = UIViewContentModeScaleAspectFit;
        
        UILabel * blankLabel = [[UILabel alloc] init];
        blankLabel.text = @"您还没有订单";
        blankLabel.font = DEF_FontSize_18;
        blankLabel.textColor = getColor(@"666666");
        blankLabel.textAlignment = NSTextAlignmentCenter;
        
        [_blankView sd_addSubviews:@[blankImage,blankLabel]];
        
        [blankImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(UNIT_HEIGHT(114));
            make.centerX.mas_equalTo(_blankView.mas_centerX);
            make.height.mas_equalTo(UNIT_HEIGHT(158));
            make.width.mas_equalTo(UNIT_WIDTH(165));
        }];
        
        [blankLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(blankImage.mas_bottom).offset(UNIT_HEIGHT(15));
            make.centerX.mas_equalTo(_blankView.mas_centerX);
            make.height.mas_equalTo(UNIT_HEIGHT(30));
            make.width.mas_equalTo(UNIT_WIDTH(150));
        }];
        
    }
    return _blankView;
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - height64 - 42) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
        }else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = [UIColor whiteColor];
    }
    return _tableView;
}

#pragma mark - <UITableViewDelegate , UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataArr.count;
    }


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MineTableViewCell * cell = [MineTableViewCell cellForTableView:tableView];
    cell.model = self.dataArr[indexPath.section];
    ws(cell)
    cell.getConfigClick = ^{
        
        weakcell.configBtn.hidden = YES;
        weakcell.comLabel.hidden = NO;
        weakcell.carImageView.hidden = NO;
        
    };
    return cell;
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UNIT_HEIGHT(330);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    OrderDetailViewController * vc = [[OrderDetailViewController alloc] init];
    
    MineDispatchModel * model = self.dataArr[indexPath.section];
    
    vc.orderNo = model.orderNo;
    
    vc.disPatchStyle = model.disttype;
    
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark -- 懒加载
- (NSMutableArray *)dataArr{
    
    if (!_dataArr) {
        
        _dataArr = @[
                     
                     ].mutableCopy;
    }
    return _dataArr;
}


@end
